package development.app;

import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.component.Event;
import state.component.EventListener;
import state.component.MultiTab;
import state.component.TextField;
import state.font.FontManager;
import state.font.ImageText;
import state.socket.DataPackage;
import state.util.GameColor;
import state.util.Hashtable;
import state.util.ImageUtil;
import state.util.array.StringArray;
import development.Constants;
import development.DrawUtils;
import development.Global;
import development.group.FrmGroupMenu;
import development.group.FrmGroupThoiTietTinTuc;

public class FrmThoiTiet extends GameForm implements EventListener {
  private final String[] TAYBACBONAME = {"Hòa bình","Lào cai","Điện biên","Sơn la","Yên bái","Lai châu"};
  private final String[] TAYBACBO = {"HOABINH","LAOCAI","DIENBIEN","SONLA","YENBAI","LAICHAU"};
  private final String[] DONGBACBONAME = {"Ninh bình","Thái bình","Hải dương","Phủ lý","Hà nội","Hưng yên","Nam định","Hà tây","Hà giang","Việt trì","Vĩnh phúc","Bắc giang","Bắc ninh","Lạng sơn","Cao bằng","Tuyên quang","Thai nguyên","Bắc cạn","Hải phòng","Quảng ninh"};
  private final String[] DONGBACBO = {"NINHBINH","THAIBINH","HAIDUONG","PHULY","HANOI","HUNGYEN","NAMDINH","HATAY","HAGIANG","VIETTRI","VINHPHUC","BACGIANG","BACNINH","LANGSON","CAOBANG","TUYENQUANG","THAINGUYEN","BACKAN","HAIPHONG","QUANGNINH"};
  private final String[] THANHHOAHUENAME = {"Hà tĩnh","Quảng bình","Quảng trị","Huế","Thanh hóa","Vinh"};
  private final String[] THANHHOAHUE = {"HATINH","QUANGBINH","QUANGTRI","HUE","THANHHOA","VINH"};
  private final String[] DANANGBINHTHUANNAME = {"Quảng ngãi","Nha trang","Bình thuận","Đà nẵng","Bình định","Phú yên","Ninh thuận","Quảng nam"};
  private final String[] DANANGBINHTHUAN = {"QUANGNGAI","NHATRANG","BINHTHUAN","DANANG","BINHDINH","PHUYEN","NINHTHUAN","QUANGNAM"};
  private final String[] TAYNGUYENNAME = {"Kon Tum","Pleiku","Daklak","Đà lạt","Dak Nông"};
  private final String[] TAYNGUYEN = {"KONTUM","PLEIKU","DAKLAK","DALAT","DAKNONG"};
  private final String[] NAMBONAME = {"Hồ chí minh","Bình dương","Long an","Đồng tháp","Tiền giang","Bến tre","Hậu giang","Cần thơ","Sóc trăng","Bạc liêu","Bình phước","Vũng tàu","Đồng nai","Tây ninh","Kiên giang","Cà mau","Vĩnh long","An giang"};
  private final String[] NAMBO = {"HOCHIMINH","BINHDUONG","LONGAN","DONGTHAP","TIENGIANG","BENTRE","HAUGIANG","CANTHO","SOCTRANG","BACLIEU","BINHPHUOC","VUNGTAU","DONGNAI","TAYNINH","KIENGIANG","CAMAU","VINHLONG","ANGIANG"};

  public String[] AREA_NAME = {"Tây Bắc Bộ", "Đông Bắc Bộ", "Thanh Hóa - T.T.Huế", "Đồng Nai - Bình Thuận", "Tây Nguyên", "Nam Bộ"};
  public StringArray AREA_ID   = new StringArray(new String[]{"1"         , "2"          , "3"                  , "4"                    , "5"         , "6"},6);
  public String[][] AREA_CODE ={TAYBACBO   , DONGBACBO    ,  THANHHOAHUE         , DANANGBINHTHUAN        , TAYNGUYEN   , NAMBO};
  public String[][] AREANAME ={TAYBACBONAME, DONGBACBONAME,  THANHHOAHUENAME     , DANANGBINHTHUANNAME    , TAYNGUYENNAME, NAMBONAME};
  
  private final int GET_THOITIET_LIST_REQUEST = 1100;
  
  private GameImage[][] itemsImages;
  private GameImage iconsearch;
  private ImageText text8;
  private int chooseIndex;
  private int HOZ_DISTANCE = 70;
  private int VER_DISTANCE = 70;
  private int DX = 50;
  private int DY = 100;
  private int NUMBER_OF_ITEM_PER_ROW = 4;
  private int numberOfItem;
  private TextField txtSearch;
  private int dx = 50;
  private int dy = 100;
  
  private static GameForm singleton;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmThoiTiet();
    }
    return singleton;
  }
  
  public void init(Hashtable hashtable) {
    iconsearch = ImageUtil.getImage(Constants.THOI_TIET_CATEGORY, "icon_search.png");
    itemsImages = new GameImage[2][6];
    numberOfItem =  AREA_ID.len();
    itemsImages[0][0] = ImageUtil.getImage(Constants.THOI_TIET_CATEGORY, "taybacbo.png");
    itemsImages[0][1] = ImageUtil.getImage(Constants.THOI_TIET_CATEGORY, "dongbacbo.png");
    itemsImages[0][2] = ImageUtil.getImage(Constants.THOI_TIET_CATEGORY, "thanhoahue.png");
    itemsImages[0][3] = ImageUtil.getImage(Constants.THOI_TIET_CATEGORY, "dongnaibinhthuan.png");
    itemsImages[0][4] = ImageUtil.getImage(Constants.THOI_TIET_CATEGORY, "taynguyen.png");
    itemsImages[0][5] = ImageUtil.getImage(Constants.THOI_TIET_CATEGORY, "nambo.png");
    for (int i = 0; i < numberOfItem; i++) {
      itemsImages[1][i] = ImageUtil.makeBrighter(itemsImages[0][i], 4);
    }
    
    txtSearch = new TextField(_manager, TextField.CONSTRAINT_USER_NAME, Constants.TEXT_FIELD_STYLE);
    if (GameConstants.IS_240x320_SCREEN) {
      txtSearch.setPosition(GameConstants.SCREEN_WIDTH / 2 - 50, 240);
      txtSearch.setSize(105, 16);
      txtSearch.setBorderColor(GameColor.BLACK_CODE);
      text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    } else {
      txtSearch.setPosition(GameConstants.SCREEN_WIDTH / 2 - 50, 190);
      txtSearch.setSize(105, 16);
      txtSearch.setBorderColor(GameColor.BLACK_CODE);
      text8 = FontManager.getFont(FontManager.FONT_SIZE_11);
    }
    NUMBER_OF_ITEM_PER_ROW = 3;
    chooseIndex = 0;
    dx =DX;
    dy =DY;
    
    DrawUtils.setBottomBar(_manager, "", "Chọn", "Trở về");
    _manager.enableTouchPad();
  }

  public void draw(Graphics g) {
    DrawUtils.drawAppBackground(g);
    if (GameConstants.IS_240x320_SCREEN) {
      txtSearch.draw(g);
      g.drawImage(iconsearch, GameConstants.SCREEN_WIDTH / 2 + 55,240, GameConstants.TOP_LEFT_ANCHOR);
      for (int i = 0; i < numberOfItem; i++) {
        int x = DX + HOZ_DISTANCE * (i % NUMBER_OF_ITEM_PER_ROW);
        int y = DY + VER_DISTANCE * (i / NUMBER_OF_ITEM_PER_ROW);
        if (chooseIndex == i) {
          g.drawImage(itemsImages[1][i], x, y, GameConstants.CENTER_ANCHOR);
          text8.drawString2(g, AREA_NAME[i], GameColor.YELLOW_CODE, GameConstants.SCREEN_WIDTH / 2,220, GameConstants.TOP_HCENTER_ANCHOR);
        } else {
          g.drawImage(itemsImages[0][i], x, y, GameConstants.CENTER_ANCHOR);
        }
      }
    } else{
      dx =90;
      dy =60;
      txtSearch.draw(g);
      g.drawImage(iconsearch, GameConstants.SCREEN_WIDTH / 2 + 55,190, GameConstants.TOP_LEFT_ANCHOR);
      for (int i = 0; i < numberOfItem; i++) {
        int x = dx + HOZ_DISTANCE * (i % NUMBER_OF_ITEM_PER_ROW);
        int y = dy + VER_DISTANCE * (i / NUMBER_OF_ITEM_PER_ROW);
        if (chooseIndex == i) {
          g.drawImage(itemsImages[1][i], x, y, GameConstants.CENTER_ANCHOR);
          text8.drawString2(g, AREA_NAME[i], GameColor.YELLOW_CODE, GameConstants.SCREEN_WIDTH / 2, 185, GameConstants.BOTTOM_HCENTER_ANCHOR);
        } else {
          g.drawImage(itemsImages[0][i], x, y, GameConstants.CENTER_ANCHOR);
        }
      }
    }
    
    // Draw command bar
    if (chooseIndex == numberOfItem) {
      DrawUtils.getBottomBar(_manager).setRightLabel("Xóa");
    } else {
      DrawUtils.getBottomBar(_manager).setRightLabel("Trở về");
    }
  }
  
  public void onActionPerform(Event event) {
    if (MultiTab.OPEN_TAB_EVENT.equals(event.getAction())) {
      DrawUtils.setBottomBar(_manager, "", "Chọn", "Trở về");
      return;
    }
  }

  public void updateFocus() {
    if (chooseIndex == numberOfItem) {
      txtSearch.setFocused(true);
    } else {
      txtSearch.setFocused(false);
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.LEFT:
      if (chooseIndex > 0) {
        chooseIndex--;
      }
      updateFocus();
      break;
    case Key.RIGHT:
      if (chooseIndex != 2 && chooseIndex != 5) {
        chooseIndex++;
      } else {
        FrmGroupThoiTietTinTuc.setTab(1);
      }
      updateFocus();
      break;
    case Key.DOWN:
      chooseIndex = (chooseIndex + NUMBER_OF_ITEM_PER_ROW > numberOfItem - 1) ? chooseIndex : chooseIndex + NUMBER_OF_ITEM_PER_ROW;
      updateFocus();
      break;
    case Key.UP:
      chooseIndex = (chooseIndex - NUMBER_OF_ITEM_PER_ROW < 0) ? chooseIndex : chooseIndex - NUMBER_OF_ITEM_PER_ROW;
      updateFocus();
      break;
    case Key.FIRE:
      FrmThoiTietTinhThanh frmThoiTietTinhThanh = (FrmThoiTietTinhThanh) FrmThoiTietTinhThanh.instance();
      if (chooseIndex == numberOfItem) {
        String code = txtSearch.getText();
        code = code.toUpperCase().trim();
        frmThoiTietTinhThanh.isshowdetail = true;
        frmThoiTietTinhThanh.title = code;
        frmThoiTietTinhThanh.type = 0;
        frmThoiTietTinhThanh.title = getNameAreaByCode(code);
        DataPackage dataPackage = DataPackage.createDataPackage1(GET_THOITIET_LIST_REQUEST);
        dataPackage.putString(code);
        Global.appClient.write(dataPackage);
      } else {
        frmThoiTietTinhThanh.type = 1;
        frmThoiTietTinhThanh.title = AREA_NAME[chooseIndex];
        frmThoiTietTinhThanh.AreaIndex = chooseIndex;
      }
      FrmGroupThoiTietTinTuc frmGroupThoiTietTinTuc = (FrmGroupThoiTietTinTuc) FrmGroupThoiTietTinTuc.instance();
      frmGroupThoiTietTinTuc.getMultiTab().setTab(0, frmThoiTietTinhThanh, null);
      break;
    case Key.SOFT_RIGHT:
      if (chooseIndex == numberOfItem) {
        txtSearch.deleteLastChar();
      }else
        GameGlobal.nextState(FrmGroupMenu.instance(), null);
      break;
    default:
      if (chooseIndex == numberOfItem) {
        txtSearch.keyReleased1(keyCode);
      }
      break;
    }
  }
  
  private String getNameAreaByCode(String code) {
    for (int i = 0; i < AREA_CODE.length; i++) {
      for (int j = 0; j < AREA_CODE[i].length; j++) {
        if (code.toLowerCase().equalsIgnoreCase(AREA_CODE[i][j])) {
          return AREANAME[i][j];
        }
      }
    }
    return "";
  }

  public void onRecieveData(DataPackage dataPackage) {
    FrmThoiTietTinhThanh frmThoiTietTinhThanh = (FrmThoiTietTinhThanh) FrmThoiTietTinhThanh.instance();
    frmThoiTietTinhThanh.onRecieveData(dataPackage);
  }
  
  public void destroy() {
    itemsImages = null;
    iconsearch = null;
    text8 = null;
    txtSearch = null;
  }
}
